﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Core.Extensions;
using App.Domain;
using App.Web.ViewModels;
using App.Web.Utility;
using AutoMapper;
using App.Core.Xml;
using System.Data;
using App.Web.ViewModels.UtilityModels;

namespace App.Web.Areas.Private.Controllers
{
    [Authorize(Roles= "Company Admin,Company User")]
    public class StoreController : BaseController
    {
		private AppEntities _Context = new AppEntities();
        
		#region Constructors
		public StoreController()
		{
		}

		public StoreController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index()
        {
            return View();
        }
		/// <summary>
        /// Gets a Store from Database to edit
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
		public ActionResult Edit(int id = 0)
        {
            Store entity = id == 0 ? new Store() : _Context.Stores.FirstOrDefault(P => P.ID == id);            
            StoreViewModel viewModel = Mapper.Map<Store, StoreViewModel>(entity);             
            return View(viewModel);
        }

		/// <summary>
        /// Saves Store Information into the Database
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
		[HttpPost]
		public ActionResult Edit(StoreViewModel viewModel)
        {
			if (!ModelState.IsValid)
                return View(viewModel);

			Store entity = null;
            if (viewModel.ID == 0)
            {
                entity = new Store();
                entity.CompanyID = LoggedInUser.Company.ID;
                entity.CreatedOn = System.DateTime.Now;
                entity.ModifiedOn = System.DateTime.Now;
                _Context.AddToStores(entity);
            }
            else
            {
                entity = _Context.Stores.FirstOrDefault(P => P.ID == viewModel.ID);
                entity.ModifiedOn = System.DateTime.Now;
            }
            entity = Mapper.Map<StoreViewModel, Store>(viewModel, entity);
            _Context.SaveChanges();
            return RedirectToAction("List");        
        }

        public ActionResult List()
        {
            return View(BuildStoreListConfig());
        }

		#endregion Action Methods
		
		#region Helper Methods

        private ListConfig BuildStoreListConfig()
        {
            UtilityDAO dao = new UtilityDAO();
            App.Domain.DbParameter[] parameters = new[]{
                new App.Domain.DbParameter("@CompanyID", LoggedInUser.Company.ID)
            };
            DataSet ds = dao.GetDataSet(DbObject.SP.GET_STORE_DETAILS, parameters, true);
            ListConfig listConfig = new ListConfig(ds.Tables[0], null);
            return listConfig;
        }

        #endregion			
    }
}
